<template>
	<view class="search">
		<view class="container">
			<scroll-view scroll-y="true">
				<view class="search-search">
					<text class="iconfont iconsearch"></text>
					<input type="text" placeholder="搜索圈子" v-model="searchWord" @confirm="handleToSearch"
						@input="handleToSuggest" />
					<text v-show="searchType == 2" @tap="handleToClose" class="iconfont iconguanbi"></text>
				</view>
				<block v-if="searchType == 1">
					<view class="search-history">
						<view class="search-history-head">
							<text>历史记录</text>
							<text class="iconfont iconlajitong" @tap="handleToClear"></text>
						</view>
						<view class="search-history-list">
							<view v-for="(item,index) in historyList" :key="index" @tap="handleToWord(item)">{{ item }}
							</view>
						</view>
					</view>
					<view class="search-hot">
						<view class="search-hot-title">推荐圈子</view>
						<view class="search-hot-item" v-for="(item,index) in dataList" :key="index"
							@tap="handleToWord(item.groupName)">
							<view class="search-hot-top">{{ index + 1 }}</view>
							<view class="search-hot-word">
								<view>
									{{ item.groupName }}
								</view>
							</view>
							<text class="search-hot-count">{{ item.groupCount || 0 }}</text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType == 2">
					<view class="search-result">
						<view class="search-result-item" v-for="(item,index) in searchList" :key="index"
							@tap="handleToDetail(item)">
							<view class="search-result-word">
								<view>{{ item.groupName }}</view>
							</view>
							<u-icon name="list-dot"></u-icon>
						</view>
					</view>
				</block>
				<block v-else-if="searchType == 3">
					<view class="search-suggest">
						<view class="search-suggest-title">搜索"{{ this.searchWord }}"</view>
						<view class="search-suggest-item" v-for="(item,index) in suggestList" :key="index"
							@tap="handleToWord(item.groupName)">
							<text class="iconfont iconsearch"></text>
							{{ item.groupName }}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		getGroupListOfSearch
	} from '@/api/bot/group'
	import '@/common/iconfont.css'
	export default {
		data() {
			return {
				searchWord: '',
				historyList: [],
				searchType: 1,
				searchList: [],
				suggestList: [],
				query: {
					isPage: false,
					groupName: '',
					isCount: false
				},
				dataList: []
			}
		},
		onLoad() {
			uni.getStorage({
				key: 'searchHistoryOfGroup',
				success: (res) => {
					console.log(res)
					this.historyList = res.data;
				}
			});
			this.getGroupList()
		},
		methods: {
			getGroupList() {
				uni.showLoading({
					title: "加载中..."
				})
				let query = {
					isCount: true,
					isPage: false
				}
				getGroupListOfSearch(query).then(res => {
					this.dataList = res.rows
					uni.hideLoading();
				})
			},
			handleToSearch() {
				if (this.searchWord) {
					this.historyList.unshift(this.searchWord);
					this.historyList = [...new Set(this.historyList)];
					if (this.historyList.length > 10) {
						this.historyList.length = 10;
					}
					uni.setStorage({
						key: 'searchHistoryOfGroup',
						data: this.historyList
					});
					this.getGroupList();
				}
			},
			handleToClear() {
				uni.removeStorage({
					key: 'searchHistoryOfGroup',
					success: () => {
						this.historyList = [];
					}
				});
			},
			handleToClose() {
				this.searchWord = '';
				this.searchType = 1;
			},
			handleToSuggest(ev) {
				let value = ev.detail.value;
				if (!value) {
					this.searchType = 1;
					return;
				}
				this.query.groupName = value;
				this.query.isCount = false;
				getGroupListOfSearch(this.query).then(res => {
					this.suggestList = res.rows
					this.searchType = 3;
				})
			},
			handleToWord(word) {
				this.searchWord = word;
				this.query.groupName = word;
				this.query.isCount = false;
				getGroupListOfSearch(this.query).then(res => {
					this.searchList = res.rows
					this.searchType = 2;
				})
			},
			handleToDetail(obj) {
				obj.router = 'other'
				const formDataStr = JSON.stringify(obj);
				this.$tab.navigateTo('/pages/detail/groupDetail?data=' + encodeURIComponent(formDataStr))
			}
		}
	}
</script>

<style scoped>
	.search-search {
		display: flex;
		background: #f7f7f7;
		height: 73rpx;
		margin: 28rpx 30rpx 30rpx 30rpx;
		border-radius: 50rpx;
		align-items: center;
	}

	.search-search text {
		margin: 0 27rpx;
	}

	.search-search input {
		font-size: 26rpx;
		flex: 1;
	}

	.search-history {
		margin: 0 30rpx;
		font-size: 26rpx;
	}

	.search-history-head {
		display: flex;
		justify-content: space-between;
	}

	.search-history-list {
		display: flex;
		margin-top: 36rpx;
		flex-wrap: wrap;
	}

	.search-history-list view {
		padding: 20rpx 40rpx;
		background: #f7f7f7;
		border-radius: 50rpx;
		margin-right: 30rpx;
		margin-bottom: 20rpx;
	}

	.search-hot {
		margin: 30rpx 30rpx;
		font-size: 26rpx;
		color: #bebebe;
	}

	.search-hot-title {}

	.search-hot-item {
		display: flex;
		align-items: center;
		margin-top: 40rpx;
	}

	.search-hot-top {
		width: 60rpx;
		color: #fb2221;
		font-size: 34rpx;
	}

	.search-hot-word {
		flex: 1;
	}

	.search-hot-word view:nth-child(1) {
		font-size: 36rpx;
		color: black;
	}

	.search-hot-word image {
		width: 48rpx;
		height: 22rpx;
	}

	.search-hot-count {}

	.search-result {
		border-top: 2rpx #e5e5e5 solid;
		padding: 30rpx;
	}

	.search-result-item {
		display: flex;
		align-items: center;
		border-bottom: 2rpx #e5e5e5 solid;
		padding-bottom: 30rpx;
		margin-bottom: 30rpx;
	}

	.search-result-item text {
		font-size: 50rpx;
	}

	.search-result-word {
		flex: 1;
	}

	.search-result-word view:nth-child(1) {
		font-size: 28rpx;
		color: #3e6694;
	}

	.search-result-word view:nth-child(2) {
		font-size: 26rpx;
	}

	.search-suggest {
		border-top: 2rpx #e5e5e5 solid;
		padding: 30rpx;
		font-size: 26rpx;
	}

	.search-suggest-title {
		color: #537caa;
		margin-bottom: 40rpx;
	}

	.search-suggest-item {
		color: #666666;
		margin-bottom: 70rpx;
	}

	.search-suggest-item text {
		color: #c2c2c2;
		font-size: 26rpx;
		margin-right: 26rpx;
	}
</style>